using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using MonoMac.Foundation;
using MonoMac.AppKit;
using VideoFlowTagger;

namespace VideoFlowTaggerMacGUI
{	
	[Register("ImageDataSource")]
	public class VideoDataSource : NSTableViewDataSource
	{
		private List<VideoFile> VideoFiles { get; set; }
	 
	    public VideoDataSource () 
		{
			this.VideoFiles = new List<VideoFile>();
	    }
		
		public VideoDataSource(List<VideoFile> videoFiles)
		{
			this.VideoFiles = videoFiles;
		}
	 
	    [Export("numberOfRowsInTableView:")]
	    public int numberOfRowsInTableView (NSTableView aTableView) {
	        if (VideoFiles == null)
	            return 0;
	        return VideoFiles.Count;
	    }
	 
	    [Export("tableView:objectValueForTableColumn:row:")]
	    public NSImage objectValueForTableColumn (NSTableView aTableView, NSTableColumn aTableColumn, int rowIndex) {
	        return new NSImage(VideoFiles[rowIndex].GetArtworkPath());
			
	    }
		
		public void SetVideoFiles(List<VideoFile> files)
		{
			this.VideoFiles = files;
		}
		
		public List<VideoFile> GetVideoFiles()
		{
			return this.VideoFiles;
		}
		
		public void AddVideoFile(VideoFile file)
		{
			this.VideoFiles.Add(file);
		}
		
		public void RemoveVideoFile(VideoFile file)
		{
			this.VideoFiles.Remove(file);
		}
		
		public VideoFile GetVideoFile(int index)
		{
			return this.VideoFiles.ElementAt(index);
		}
		
		public int IndexOf(VideoFile file)
		{
			return this.VideoFiles.IndexOf(file);
		}
	}
	 
	public class VideoDataSourceDelegate : NSTableViewDelegate 
	{
	    public event EventHandler<VideoChangedEventArgs> VideoChanged;
	 
	    public VideoDataSourceDelegate () : base()  
		{
	    }
	 
	    public override void SelectionDidChange(NSNotification notification) 
		{
	        var table = notification.Object as NSTableView;
	           
	        var ds = table.DataSource as VideoDataSource;
	           
	        var rowNum = table.SelectedRow;
	           
	        if (rowNum >= 0 && rowNum < ds.GetVideoFiles().Count)
	            OnVideoFileChanged(new VideoChangedEventArgs(ds.GetVideoFiles()[rowNum]));
	    }
	       
	    protected void OnVideoFileChanged(VideoChangedEventArgs e) 
		{
	        if (VideoChanged != null)
	            VideoChanged(this, e);
	    }
	}
	   
	public class VideoChangedEventArgs : EventArgs 
	{
		public VideoFile File { get; set; }
	       
	    public VideoChangedEventArgs(VideoFile t) {
			File = t;
	    }
	}	
}

